package j2024.j202410;

import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class j1029 {
    //天使果冻
    public static void main11(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = arr[0];
        g[0] = Integer.MIN_VALUE;
        for (int i = 1; i < n; i++) {
            int x = arr[i];
            f[i] = Math.max(f[i-1],x);
            if(x>=f[i-1]){
                g[i] = f[i-1];
            }else if(f[i-1]>x && x>g[i-1]){
                g[i] = x;
            }else{
                g[i] = g[i-1];
            }
        }
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            int tmp = in.nextInt();
            System.out.println(g[tmp-1]);
        }
    }
//    public static long secondMax(long[] arr,long cur,long max,int index){
//        for (int i = 0; i < index; i++) {
//            if(arr[i]>max){
//                cur = max;
//                max = arr[i];
//            }else if(arr[i]>cur && arr[i]<max){
//                cur = arr[i];
//            }else if(arr[i]==max){
//                cur = max;
//            }
//        }
//        return cur;
//    }
//    public static void xz180(int[][] arr,int n){
//        for (int i = 0; i <= n/2; i++) {
//            for (int j = 0; j < n-i-1; j++) {
//                int tmp = arr[n-1-i][n-1-j];
//                arr[n-1-i][n-1-j] = arr[i][j];
//                arr[i][j] = tmp;
//            }
//        }
//        for (int i = 0; i < n; i++) {
//            for (int j = 0; j < n; j++) {
//                System.out.print(arr[i][j]+" ");
//            }
//            System.out.println();
//        }
//    }
//    public static void jx(){
//
//    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(),k = in.nextInt();
        long[] a = new long[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = in.nextLong();
        }
        long[][] dp = new long[n+1][k];
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j < k; j++) {
                dp[i][j] = (long)-1e16;
            }
        }
        dp[0][0] = 0;
        for (int i = 1; i <=n ; i++) {
            for (int j = 0; j < k; j++) {
                dp[i][j] = Math.max(dp[i-1][j],dp[i-1][(int)((j-a[i]%(long)k+(long)k)%(long)k)]+a[i]);
            }
        }
        if(dp[n][0]>0){
            System.out.println(dp[n][n]);
        }else {
            System.out.println(-1);
        }
    }
    /**
     * 61. 旋转链表
     * 给你一个链表的头节点 head ，旋转链表，将链表每个节点向右移动 k 个位置。
     * @param head
     * @param k
     * @return
     */
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null || head.next==null ||k==0){
            return head;
        }
        ListNode ret = new ListNode(-1);
        int cou = 1;
        ListNode tmp = head;
        while(tmp.next!=null){
            cou++;
            tmp = tmp.next;
        }
        int n = cou-k%cou;
        if(n==cou){
            return head;
        }
        //成环
        tmp.next = head;
        //新链表的最后一个节点（即原链表的第 (n−1)−(k%n) 个节点）
        for (int i = 0; i < n; i++) {
            tmp = tmp.next;
        }
        ret.next = tmp.next;
        tmp.next = null;
        return ret;
    }
}
class Main
{
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();

    public static void main(String[] args) throws IOException
    {
        // 写代码
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        int q = in.nextInt();
        int row = 0,col = 0;
        for (int i = 0; i < q; i++) {
            int tmp = in.nextInt();
            if(tmp==1){
                row++;col++;
            }else if(tmp==2){
                row++;
            }
        }
        row%=2;col%=2;
        //行变换
        if(row==1){
            for (int i = 0; i < n/2; i++) {
                for (int j = 0; j < n; j++) {
                    int t = arr[i][j];
                    arr[i][j] = arr[n-i-1][j];
                    arr[n-i-1][j] = t;
                }
            }
        }
        if(col==1){
            for (int j = 0; j < n/2; j++) {
                for (int i = 0; i < n; i++) {
                    int t = arr[i][j];
                    arr[i][j] = arr[i][n-1-j];
                    arr[i][n-1-j] = t;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                out.print(arr[i][j]+" ");
            }
            out.println();
        }
        out.close();
    }
}


class Read // 自定义快速读入
{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException
    {
        while(!st.hasMoreTokens())
        {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }

    String nextLine() throws IOException
    {
        return bf.readLine();
    }

    int nextInt() throws IOException
    {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException
    {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException
    {
        return Double.parseDouble(next());
    }
}